5. SpringBoot使用jms錯誤處理延(1)


目標:可以讓jmsTemplate 可以針對不同類型的錯誤進行處理

前情提要:
銜接上1篇文章分析1下
之前介紹的主要是 接收所有Exception
而馬上要介紹的,分類進行錯誤處理

必須知識:

java 8
spring系列
MVC 分層抽離
mq的基本知識
可能需要會通靈,解釋性資訊不多XD
spring boot核心應用為以下

  • spring starter
  • mq spring starter
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.11</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependency>
    <groupId>com.ibm.mq</groupId>
    <artifactId>mq-jms-spring-boot-starter</artifactId>
    <version>2.0.0</version>
    </dependency>
    
    現在來分類,舉例來說:
  1. ApException: 商業邏輯上的錯誤
  2. DocException: 文件上下傳的錯誤
  3. AccountException: 帳號處理的錯誤
  4. JMSException: jmsTemplate預設的錯誤

以上這些錯誤如果都根據這些情境進行分類是不是就可以很方便除錯
不過這是理想的情境預設是只有JMSException其他都是要我們定義的

上方那個很抽象這邊簡易說明一下

Throwable retrieved = t.getCause();
Exception exception = (Exception) retrieved;

這是上一篇簡單展示可以顯示的log方式

聰明的大家應該可以發現,這邊是使用強制轉型的方式
這邊其實就是我們可以宣告多種不同的Exception
然後用強制轉型的方式進行不同的錯誤處理

片段(1)
我們可以用以下方式來區分

 if (throwable instanceof ApException) {
       ApException apException = (ApException) throwable;
    }
 if (throwable instanceof DocException) {
       DocException docException = (DocException) throwable;
    }

以下為不同的材料

材料(1):
那這樣我們要怎麼讓throwable 認得(instanceof)這些Exception
可以參考以下

public class ApException extends RuntimeException {

    // 這邊省略很多不同的步驟

    public ApException(Throwable cause) {
        super(cause);
    }

    public ApException(String message, Throwable cause) {
        super(message, cause);
    }
}

因為我之前是spring web 有時候會客製化Response
所以這邊我就沿用這個概念在這了,另外JSON是輕量化易讀性高的一種文本形式
我們再把Exception放進這文本中來作為log

材料(2):
ContextJson.class


@Data
public class ContextJson<T> {

    private int status;
    private String message;
    private T data;
    private Exception ex;

    public static <T> ContextJson<T> success(T data, String message) {
        ContextJson<T> response = new ContextJson<>();
        response.status = "OK";
        response.data = data;
        response.message = message;
        return response;
    }

    public static <T> ContextJson<T> success() {
        ContextJson<T> response = new ContextJson<>();
        response.status = "OK";
        return response;
    }

    public static ContextJson<Void> failure(String message, Exception ex) {
        ContextJson<Void> response = new ContextJson<>();
        response.status = "FAIL";
        response.message = message;
        response.ex = ex;
        return response;
    }

    }

以上這些都算是材料
現在要來組成和註冊
JmsErrorHandler.class 請參考上一篇的進行修改

@Slf4j
@Configuration
public class JmsErrorHandler implements ErrorHandler {

    private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();

    @Override
    public void handleError(Throwable t) {
        Throwable retrieved = t.getCause();
        // 我們把剛剛的片段(1)擺在這裡
         if (throwable instanceof ApException) {
           ApException apException = (ApException) throwable;
           log.info(gson.toJson(ContextJson.failure("商業邏輯錯誤",apException))); // 因為此範例會將錯誤顯示在server log/ console
            }
         if (throwable instanceof DocException) {
           DocException docException = (DocException) throwable;
           log.info(gson.toJson(ContextJson.failure("文件輸出輸入錯誤",docException))); // 因為此範例會將錯誤顯示在server log/ console
            }
    }
}

應用時
JmsMessageService省略介面
直接實作

@Service
public JmsMessageServie implment JmsMessageService{

if(Bollean.FALSE.equals(sthFlag)){
    throw new ApException("業務邏輯錯誤");
}

}

這邊進行了1個分類
那下1篇會更抽象的1個層次

補充:

需要有mq服務 建議可以使用containter方便又快速

主要是給自己的一個紀錄,也分享給有需要的夥伴
註解部分有提及一些參考的連結,有興趣可以點進去看看喔

這是一個心血來潮,產生的文章
若有喜歡或交流的部分都歡迎在下方留言,多多關照。

#mq #jmstemplate







你可能感興趣的文章

網路基礎概論-http協定

網路基礎概論-http協定

Redux, useSelector, useDispatch

Redux, useSelector, useDispatch

[week 1] 版本控制 - Git 概念 & 基本指令

[week 1] 版本控制 - Git 概念 & 基本指令






留言討論